import Vue from 'vue'
import App from './App'
import store from './store'
import 'common/base.less'
import 'common/icono.min.css'
import {pathToBase64,base64ToPath} from 'image-tools'

Vue.prototype.$store = store
Vue.prototype.formatTime = function (time,format){
	 var date  = new Date(time)   
	 var formatObj = { y: date.getFullYear(), m: date.getMonth() + 1, d: date.getDate(), h: date.getHours(), i: date.getMinutes(), s: date.getSeconds(), a: date.getDay() }  
	 return (format || '{h}:{i} {m}/{d}/{y}').replace(/{(y|m|d|h|i|s|a)+}/g, (result, key) => { let value = formatObj[key]; return value < 10 ? '0' + value :  value  })  
},
Vue.prototype.formatWithComma = function (n,m=2,g=true){
	  return new Number(n).toLocaleString('zh', { minimumFractionDigits: m, useGrouping: g }) // 向上省略	  
},
Vue.prototype.navigateTo = function(url){uni.navigateTo({url})}
Vue.prototype.formatDecimal = function (num,decimal){
    if(num == null){
	   return parseFloat(0).toFixed(decimal)
	  }
	  num = num.toString()
	  let index = num.indexOf('.')
	  if (index !== -1) {
	      num = num.substring(0, decimal + index + 1)
	  } else {
	      num = num.substring(0)
	  }
	  var s = parseFloat(num).toFixed(decimal +1)
	  return s.substr(0,s.length-1)
},

Vue.prototype.regPhone = function (str){ 
	if(!(/^1[34578]\d{9}$/.test(str))){
	   uni.showToast({title: '请输入正确的手机号',icon:'none',duration: 2000})
	   return true;
	}  
},
Vue.prototype.regEmail = function (str){  
	if(!(/^([a-zA-Z]|[0-9])(\w|\-)+@[a-zA-Z0-9]+\.([a-zA-Z]{2,4})$/.test(str))){
		uni.showToast({title: '请输入正确的邮箱',icon:'none',duration: 2000}) 
		return true;
	} 
},

Vue.prototype.regPassword = function (str){ 
	if (!(/^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/.test( str ))) {
		 uni.showToast({ title: '密码需要，6—16位数字加字母', icon: "none" }); 
		return true;
	}
},
Vue.prototype.isEmpty = function (s){
	return !s || /^\s*$/g.test(s)
} 

Vue.prototype.copyData = function (str,msg="复制成功"){  console.log("copy str: ", str);
	uni.setClipboardData({data: str,success: function () {uni.showToast({title: msg,duration: 2000});}});
},
Vue.prototype.formatNum = function(e,tail) {
	e.target.value = e.target.value.replace(/[^0-9.]/ig,"")
	var a = e.target.value.split(".")[0] ,b =  e.target.value.split(".")[1]
	if(b){
		b = b.slice(0,tail)
		e.target.value  = a +'.' + b
	}
	return { a , b }
}  
Vue.prototype.saveQrcode = function(img) {  
	 base64ToPath(img).then(path => {
	 		uni.saveImageToPhotosAlbum({ filePath: path,success: function() { uni.showToast({title: '保存成功',duration: 2000});}, 
			fail: function() { uni.showToast({title: '保存失败',duration: 2000});}});
	 }) .catch(error => {
		 console.log(error) 
		 uni.showToast({title: '载入失败', icon:'none',duration: 2000});
	 })
}  

App.mpType = 'app'

const app = new Vue({
	store,
    ...App
})
app.$mount()
